package assist.datastore.parser;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

import java.util.logging.Logger;

import assist.datastore.dao.DAOFactory;
import assist.datastore.dao.PlayerDAO;
import assist.datastore.dao.TeamDAO;
import assist.datastore.dao.exception.MultiplePlayersFoundException;
import assist.datastore.dao.exception.PlayerNotFoundException;
import assist.domain.model.Player;
import assist.domain.model.RosterPlayer;
import assist.domain.model.Team;

public class NHLRostersCSVParser extends NHLCSVParser<RosterPlayer>
{
    private static final long serialVersionUID = 1L;
    private static Logger logger = 
        Logger.getLogger(NHLRostersCSVParser.class.getName());
    private Set<String> missingPlayers;    
    
    public NHLRostersCSVParser(final File csvFile, Integer startRecord) throws IOException
    {
       super(csvFile, startRecord);
       for(String player : missingPlayers)
       {
           logger.fine("Player Missing," + player);          
       }
    }

    @Override
    protected RosterPlayer parseObject(String[] tokens)
    {
        if (missingPlayers == null)
        {
            missingPlayers = new TreeSet<String>(); 
        }
        RosterPlayer rosterPlayer = new RosterPlayer();
        if (tokens[5].length() != 1)
        {
            return null;
        }
        rosterPlayer.setPosition(tokens[5]);
        
        rosterPlayer.setNumber(new Integer(tokens[4].split("\\.")[0]));
        Integer year = new Integer(tokens[0].split("\\.")[0]);
        Integer gameId = new Integer(tokens[1].split("\\.")[0]);
        rosterPlayer.setGameId(year + "" + gameId);
        String name = tokens[6].split("\\(")[0].trim();
        //TODO: Look up team abbereviation.
        TeamDAO teamDao = DAOFactory.getDAOFactory().getTeamDAO();
        Team team = teamDao.getByTeamName(tokens[3]);
        rosterPlayer.setTeam(team);
        
        PlayerDAO playerDAO = DAOFactory.getDAOFactory().getPlayerDAO();
       // Player player = playerDAO.getByName(name);
        
        Player player;
        try
        {
            player = playerDAO.getByName(name);
            if (player == null)
            {               
                logger.warning(name + "," + "Alias ID is not in Players");
                return null;                
            }             
            rosterPlayer.setPlayer(player);
        }
        catch (MultiplePlayersFoundException e)
        {
            player = playerDAO.getPlayerByYear(e.getPlayerIds(), year);
            if (player == null)
            {
                logger.warning(name + "," + "Multiple, not found in PlayerYear");                
                return null;
            }             
            rosterPlayer.setPlayer(player);
        }
        catch (PlayerNotFoundException e)
        {
            missingPlayers.add(e.getPlayerName());
            return null;
        }
       
        rosterPlayer.setKey(rosterPlayer.getGameId() + "" + rosterPlayer.getPlayer().getPlayerId());
        return rosterPlayer;
    }

}
